<?php

namespace app\common\service;
use app\common\service\Base;

class OrderDelivery extends Base
{

	/**
	 * 根据订单ID获取发货单列表
	 *
	 * @author KING
	 * @date 2020年7月9日
	 *
	 * @param integer $orderId			订单ID
	 * @param string $field				查询的字段
	 * @param string $decodeJson		是否反序列化JSON
	 * @return array
	 */
	public function getListByOrderId($orderId, $field='*', $decodeJson=true) {
		if (empty($orderId)) return [];
		$list = db('order_delivery')->where('order_id', $orderId)->where('delivery_status', '<>', 99)->field($field)->select();
		if ($decodeJson) {
			foreach ($list as $k => $v) {
				if (array_key_exists('delivery_detail', $v)) $list[$k]['delivery_detail'] = $this->_formatSizeCount($v['delivery_detail']);
				if (array_key_exists('return_detail', $v)) $list[$k]['return_detail'] = $this->_formatSizeCount($v['return_detail']);
				if (array_key_exists('store_detail', $v)) $list[$k]['store_detail'] = $this->_formatSizeCount($v['store_detail']);
				if (array_key_exists('real_shipment_detail', $v)) $list[$k]['real_shipment_detail'] = $this->_formatSizeCount($v['real_shipment_detail']);
			}
		}
		return $list;
	}

	/**
	 * 获取指定状态的mes发货数
	 *
	 * @author KING
	 * @date 2020年7月22日
	 *
	 * @param integer|array $orderIds	订单ID
	 * @param array $deliveryStatusArr	发货单状态
	 * @return number
	 */
	public function getDeliveryCount($orderIds, $deliveryStatusArr=[]) {
		if (is_array($orderIds)) {
			$where[] = ['order_id', 'in', $orderIds];
		} else {
			$where[] = ['order_id', '=', $orderIds];
		}
		if ($deliveryStatusArr != '') $where[] = ['delivery_status', 'in', $deliveryStatusArr];
		return db('order_delivery')->where($where)->sum('delivery_count');
	}

	/**
	 * 获取指定时间段内的出货数
	 *
	 * @author King
	 * @date 2021年9月23日
	 *
	 * @param integer $startTime
	 * @param integer $endTime
	 * @param integer $factoryId
	 * @param string $andWhere
	 * @param integer $isAutoImport
	 * @return number
	 */
	public function getRealShipmentCountByRange($startTime, $endTime, $factoryId=0, $andWhere='', $isAutoImport=0) {
		$where = [
			['o.status', 'in', [0,1,2]],
			['od.delivery_time', 'between', [$startTime, $endTime]],
			['od.delivery_status', 'not in', [6, 7]],
		];
		if ($isAutoImport) {
			$where[] = ['o.is_auto_import', '=', $isAutoImport];
		}
		if ($factoryId != '') $where[] = ['o.factory_id', is_numeric($factoryId)?'=':'in', $factoryId];
		return db('order_delivery od')->join('order o', 'o.id=od.order_id')->where($where)->where($andWhere)->sum('od.real_shipment_count');
	}

	/**
	 * 获取指定时间段内的出货加工费
	 *
	 * @author King
	 * @date 2021年9月23日
	 *
	 * @param integer $startTime
	 * @param integer $endTime
	 * @param integer $factoryId
	 * @param string $andWhere
	 * @param integer $isAutoImport
	 * @return number
	 */
	public function getRealShipmentCostByRange($startTime, $endTime, $factoryId=0, $andWhere='', $isAutoImport=0) {
		$where = [
			['o.status', 'in', [0,1,2]],
			['od.delivery_time', 'between', [$startTime, $endTime]],
			['od.delivery_status', 'not in', [6, 7]],
		];
		if ($isAutoImport) {
			$where[] = ['o.is_auto_import', '=', $isAutoImport];
		}
		if ($factoryId != '') $where[] = ['o.factory_id', is_numeric($factoryId)?'=':'in', $factoryId];
		return db('order_delivery od')->join('order o', 'o.id=od.order_id')->join('sku s', 'o.sku=s.sku')->where($where)->where($andWhere)->sum('od.real_shipment_count*s.cost');
	}

	/**
	 * 格式化尺码对应的数量
	 *
	 * @author King
	 * @date 2021年8月3日
	 *
	 * @param unknown $str
	 * @return mixed[]
	 */
	private function _formatSizeCount($str) {
		if (empty($str)) return [];
		$json = json_decode($str, true);
		$result = [];
		foreach ($json as $k => $v) {
			$result[$v['sizeName']] = $v['quantity'];
		}
		return $result;
	}
}